package com.buddy.qreport.datasource.dao;

import com.buddy.qreport.common.mybatis.entity.Page;
import com.buddy.qreport.datasource.entity.DataSourceEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

@Mapper
public interface IDataSourceMapper {



    @Select("select exists(select datasource_id from t_datasource where datasource_id = #{id})")
    boolean exists(String id);

    @Select("select * from t_datasource")
    List<DataSourceEntity> getList(Page page);


    @SelectKey(statementType = StatementType.PREPARED, resultType = String.class,keyProperty = "datasourceId",statement = "select replace(uuid(),'-','') from dual ",before = true)
    @Insert({"insert into t_datasource ","" +
            "(datasource_id,datasource_url,datasource_username,datasource_password,datasource_name,datasource_desc,datasource_driver)",
            "values(#{datasourceId},#{datasourceUrl},#{datasourceUsername},#{datasourcePassword},#{datasourceName},#{datasourceDesc},#{datasourceDriver})"

    })
    void add(DataSourceEntity entity);


    @Select("select * from t_datasource where datasource_id = #{id}")
    DataSourceEntity getOne(String id);

    @Delete("delete from t_datasource where datasource_id =#{id}")
    void delete(String id);

    @Update({"update t_datasource set ",
            "datasource_url=#{datasourceUrl},",
            "datasource_username=#{datasourceUsername},",
            "datasource_password=#{datasourcePassword},",
            "datasource_name=#{datasourceName},",
            "datasource_desc=#{datasourceDesc},",
            "datasource_driver=#{datasourceDriver}",
            "where datasource_id = #{datasourceId}"
    })
    void updateById(DataSourceEntity entity);
}
